package correct.day11;

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++)
        {
            arr[i] = in.nextInt();
        }
        int ret = 0;
        for(int i = 1; i < n; i++)
        {
            if(arr[i] > arr[i - 1])
            {
                ret += arr[i] - arr[i - 1];
            }
        }
        System.out.println(ret);
    }
}
